package com.wms.controller;

import com.wms.context.BaseContext;
import com.wms.dto.UserPageQueryDTO;
import com.wms.entity.WmsMenu;
import com.wms.entity.WmsType;
import com.wms.entity.WmsUser;
import com.wms.result.PageResult;
import com.wms.result.Result;
import com.wms.service.MenuService;
import com.wms.service.UserService;
import com.wms.vo.UserRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户管理模块")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;

    /**
     * 登录
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result login(@RequestBody WmsUser user, HttpSession session){
        log.info("用户登录：{}",user);
        UserRoleVo userRoleVo = new UserRoleVo();
        BeanUtils.copyProperties(user,userRoleVo);
        UserRoleVo info = userService.login(userRoleVo);
        if (info != null){
            Map map = new HashMap();
            map.put("user",info);
            List<WmsMenu> all = menuService.findAll(info.getUserName());
            map.put("menus",all);
            session.setAttribute("user",info);
            BaseContext.setCurrentId(info.getUserId());
            return Result.success(map);
        }
        return Result.error("用户名或密码错误或账号已被禁用！");
    }

    /**
     * 分页查询
     * @param userPageQueryDTO
     * @return
     */
    @PostMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> list(@RequestBody UserPageQueryDTO userPageQueryDTO){
        PageResult pageResult = userService.pageQuery(userPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/update")
    @ApiOperation("修改用户信息")
    public Result update(@RequestBody WmsUser user){
        log.info("修改用户信息：{}",user);
        userService.update(user);
        return Result.success();
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @PostMapping("/sign")
    @ApiOperation("注册用户")
    public Result sign(@RequestBody WmsUser user){
        log.info("保存用户信息：{}",user);
        userService.sign(user);
        return Result.success();
    }

    /**
     * 批量删除用户
     * @param ids
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation("批量删除用户")
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除用户信息：{}",ids);
        userService.delete(ids);
        return Result.success();
    }

    /**
     * 分页查询管理员信息
     * @param userPageQueryDTO
     * @return
     */
    @PostMapping("/pageAdmin")
    @ApiOperation("分页查询管理员信息")
    public Result<PageResult> pageAdmin(@RequestBody UserPageQueryDTO userPageQueryDTO){
        log.info("分页查询管理员信息：{}",userPageQueryDTO);
        userPageQueryDTO.setRoleId(2L);
        PageResult pageResult = userService.pageQuery(userPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 分页查询员工信息
     * @param userPageQueryDTO
     * @return
     */
    @PostMapping("/pageEmployee")
    @ApiOperation("分页查询员工信息")
    public Result<PageResult> pageEmployee(@RequestBody UserPageQueryDTO userPageQueryDTO){
        log.info("分页查询用户信息：{}",userPageQueryDTO);
        userPageQueryDTO.setRoleId(3L);
        PageResult pageResult = userService.pageQuery(userPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 分页查询用户信息
     * @param userPageQueryDTO
     * @return
     */
    @PostMapping("/pageUser")
    @ApiOperation("分页查询用户信息")
    public Result<PageResult> pageUser(@RequestBody UserPageQueryDTO userPageQueryDTO){
        log.info("分页查询用户信息：{}",userPageQueryDTO);
        userPageQueryDTO.setRoleId(4L);
        PageResult pageResult = userService.pageQuery(userPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 保存角色
     * @param userRoleVo
     * @return
     */
    @PostMapping("/saveRole")
    @ApiOperation("保存角色")
    public Result saveRole(@RequestBody UserRoleVo userRoleVo){
        log.info("保存角色：{}",userRoleVo);
        int i = userService.saveRole(userRoleVo);
        if (i == 0){
            return Result.error("保存失败");
        }
        return Result.success();
    }

    /**
     * 修改用户信息和角色
     * @param userRoleVo
     * @return
     */
    @PutMapping("/updateRole")
    @ApiOperation("修改用户信息和角色")
    public Result updateRole(@RequestBody UserRoleVo userRoleVo){
        log.info("修改用户信息：{}",userRoleVo);
        int i = userService.updateRole(userRoleVo);
        if (i == 0){
            return Result.error("修改失败");
        }
        return Result.success();
    }


    /**
     * 查询所有员工
     * @return
     */
    @GetMapping("/getAll")
    @ApiOperation("查询所有用户")
    public Result getAll(){
        List<WmsUser> wmsUsers = userService.getAll();
        return Result.success(wmsUsers);
    }
}
